package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ServiceEntity;

/**
 * Created by qiuzhenhuan on 2019/6/17.
 */

public class ADA_ReserveDetailService extends RecyclerView.Adapter<ADA_ReserveDetailService.ViewHolder> {

    private Context mContext;

    private List<ServiceEntity> mData;

    private boolean isClosed;

    private boolean isMember;

    public ADA_ReserveDetailService(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ADA_ReserveDetailService.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_reserve_detail_service, parent, false);
        return new ADA_ReserveDetailService.ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ADA_ReserveDetailService.ViewHolder holder, int position) {
        final ServiceEntity serviceEntity = mData.get(position);
        holder.tvServiceName.setText(StringUtils.get(serviceEntity.getServiceName()));
        holder.tvServicePrice.setText(StringUtils.getFormattedMoneyWithUnit(isMember ? serviceEntity.getMemberPrice() : serviceEntity.getOriginPrice()));
        holder.tvServerInfo.setText(String.format("%s（%s，%s分钟）", StringUtils.get(serviceEntity.getTechnicianName()),
                StringUtils.get("游泳师"), StringUtils.get(serviceEntity.getServiceTime())));

        if (!StringUtils.isEmpty(serviceEntity.getSuborderStatus()) && "3".equals(serviceEntity.getSuborderStatus())) {
            holder.tvCancelHold.setBackgroundResource(R.color.bg_color);
            holder.tvCancelHold.setText("已解除占用");
            holder.tvCancelHold.setTextColor(mContext.getResources().getColor(R.color.order_status_reserve));
            holder.tvCancelHold.setOnClickListener(null);
        } else {
            holder.tvCancelHold.setBackgroundResource(R.drawable.bg_shape_cancel_hold);
            holder.tvCancelHold.setText("解除占用");
            holder.tvCancelHold.setTextColor(mContext.getResources().getColor(R.color.order_status_service));
            holder.tvCancelHold.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemCancelHoldListener != null) {
                        onItemCancelHoldListener.onItemCancelHold(serviceEntity);
                    }
                }
            });
        }
        holder.tvCancelHold.setVisibility(isClosed ? View.GONE : View.VISIBLE);
    }

    public void setData(List<ServiceEntity> data) {
        this.mData = data;
    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 0 : mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_service_name)
        TextView tvServiceName;
        @BindView(R.id.tv_service_price)
        TextView tvServicePrice;
        @BindView(R.id.tv_server_info)
        TextView tvServerInfo;
        @BindView(R.id.tv_cancel_hold)
        TextView tvCancelHold;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setMember(boolean member) {
        isMember = member;
    }

    public void setClosed(boolean closed) {
        isClosed = closed;
    }

    private OnItemCancelHoldListener onItemCancelHoldListener;

    public void setOnItemCancelHoldListener(OnItemCancelHoldListener onItemCancelHoldListener) {
        this.onItemCancelHoldListener = onItemCancelHoldListener;
    }

    public interface OnItemCancelHoldListener {

        void onItemCancelHold(ServiceEntity serviceEntity);
    }
}
